package com.aijk.commons.lock;

import com.aijk.conifg.PublicConfig;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * <p>  </p>
 * User: chendw
 * Date: 2016/8/30 9:07
 */
public class RedisServer {

    public static JedisCluster getConfigCluster() {
        return getCluster(PublicConfig.PUBLIC_SYSTEM.get("redis.hosts"));

    }


    public static JedisCluster getCluster(String redisHosts) {
        Set<HostAndPort> hostSet = new HashSet<>();


        String[] array = StringUtils.split(redisHosts, ",");
        for (int i = 0; i < array.length; i++) {
            String[] temp = StringUtils.split(array[i], ":");
            hostSet.add(new HostAndPort(temp[0], Integer.parseInt(temp[1])));
        }
        return new JedisCluster(hostSet);
    }

    public static String getConfig() {
        return PublicConfig.PUBLIC_SYSTEM.get("redis.hosts");
    }
}
